<template>
    <div>
        <el-row class="title">
            <h3>视频管理</h3>
        </el-row>
        <el-row>
            <el-form :inline="true">
                <el-form-item>
                    <el-button type="primary" @click="showDialog"><i class="el-icon-plus el-icon--left"></i>添加视频</el-button>
                </el-form-item>
                <el-form-item label="视频名">
                    <el-input class="search-input" v-model="videoNameLike" placeholder="请输入视频名称" clearable @change="getData"></el-input>
                </el-form-item>
                <el-form-item label="视频分类">
                    <el-select class="search-input" v-model="videoCategoryId" placeholder="请选择视频分类" @change="getData">
                        <el-option v-for="item in videoCateGoryList" :key="item.videoCategoryId" :label="item.videoCategoryName" :value="item.videoCategoryId">
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-form>
        </el-row>
        <el-row>
            <el-table :data="videoList" border>
                <el-table-column prop="videoId" label="id" align="center"></el-table-column>
                <el-table-column prop="videoName" label="视频名" align="center"></el-table-column>
                <el-table-column prop="videoName" label="视频名" align="center"></el-table-column>
                <el-table-column prop="videoCategoryName" label="所属分类" align="center"></el-table-column>
                <el-table-column prop="particular" label="视频描述" align="center"></el-table-column>
                <el-table-column prop="orderNumber" label="排序值" align="center"></el-table-column>
                <el-table-column label="添加时间" align="center">
                    <template slot-scope="scope">
                        {{moment(scope.row.addTime).format('YYYY-MM-DD HH:mm:ss')}}
                    </template>
                </el-table-column>
                <el-table-column label="视频封面" align="center" width="200px">
                    <template slot-scope="scope">
                        <img v-if="scope.row.coverTheAddress" :src="getImageUrl(scope.row.coverTheAddress)" alt="" class="poster">
                    </template>
                </el-table-column>
                <el-table-column label="视频地址" align="center">
                    <template slot-scope="scope">
                        <a v-if="scope.row.videoUrl" :href="getImageUrl(scope.row.videoUrl)" target="_blank">{{getImageUrl(scope.row.videoUrl)}}</a>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center" width="160px">
                    <template slot-scope="scope">
                        <el-button size="small" type="primary" @click="updateDatas(scope.$index, scope.row)">修改</el-button>
                        <el-button size="small" type="danger" @click="deleteDatas(scope.$index, scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-row>
        <el-dialog class="dialog" :title="add ? '添加' : '修改' + '视频'" :visible.sync="dialogShow" width="600px" @close="closeDialog">
            <el-form label-width="120px">
                <el-form-item label="视频名称">
                    <el-input v-model="video.videoName" placeholder="视频名称"></el-input>
                </el-form-item>
                <el-form-item label="视频分类">
                    <el-select v-model="video.videoCategoryId" placeholder="请选择视频分类">
                        <el-option v-for="item in videoCateGoryList" :key="item.videoCategoryId" :label="item.videoCategoryName" :value="item.videoCategoryId">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="视频描述">
                    <el-input type="textarea" row="3" v-model="video.particular" placeholder="视频描述"></el-input>
                </el-form-item>
                <el-form-item label="视频">
                	<el-switch
					  v-model="uploadVideo"
					  active-color="#000"
					  inactive-color="#ff4949"
					  active-text="上传视频"
					  inactive-text="输入地址"
					  @change="changeUpload">
					</el-switch>
                    <el-upload v-if="uploadVideo" :headers="uploadHeader" :action="BASEURL + '/upload/single/video'" :show-file-list="false" :on-success="uploadVideoSuccess" accept="video/*" :on-progress="uploadVideoProgress">
                        <el-button size="small" type="primary" :loading="videoLoading">上传视频</el-button>
                    </el-upload>
                    <el-input v-else v-model="video.videoUrl" placeholder="请输入视频地址"></el-input>
                    <el-progress v-if="videoLoading == true" :percentage="videoUploadPercent"></el-progress>
                    <div v-if="videoUrl">
                        <video :src="uploadVideo ? getImageUrl(videoUrl) : videoUrl" width="100%" controls></video>
                    </div>
                </el-form-item>
                <el-form-item label="视频封面">
                    <el-upload :headers="uploadHeader" :action="BASEURL + '/upload/single/poster'" :show-file-list="false" :on-success="uploadPosterSuccess"  accept="image/*" :on-progress="uploadImageProgress">
                        <el-button size="small" type="primary" :loading="imageLoading">上传封面</el-button>
                    </el-upload>
                    <el-progress v-if="imageLoading == true" :percentage="imageUploadPercent"></el-progress>
                    <div v-if="imageUrl">
                        <img :src="getImageUrl(imageUrl)" alt="" class="poster-thumb">
                    </div>
                </el-form-item>
                <el-form-item label="排序值">
                    <el-input-number v-model="video.orderNumber" :min="1" label="排序值"></el-input-number>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="closeDialog">取 消</el-button>
                <el-button type="primary" @click="save" :loading="loading">确 定</el-button>
            </div>
        </el-dialog>
        <!-- 分页 -->
        <el-row>
            <el-col class="pagination">
                <el-pagination @current-change="currentChange" :current-page="pageId" :page-size="pageSize" layout="total, prev, pager, next" :total="count">
                </el-pagination>
            </el-col>
        </el-row>
    </div>
</template>
<script>
import {
    getVideoCategoryLists,
    getVideoLists,
    addVideo,
    deleteVideoById,
    updateVideoById
} from '@/api/api'
export default {
    data: function() {
        return {
            videoCateGoryList: [],
            videoList: [],
            add: true,
            dialogShow: false,
            loading: false,
            videoCategoryName: '',
            videoCategoryId: null,
            pageId: 1,
            pageSize: 10,
            count: 0,
            videoNameLike: '',
            uploadHeader: {
                TOKEN: sessionStorage.getItem('jwt')
            },
            video: {
                videoId: null,
                videoName: '',
                orderNumber: 1,
                particular: '',
                coverTheAddress: '',
                videoCategoryId: null,
                videoUrl: ''
            },
            imageUrl: '',
            videoUrl: '',
            uploadVideo: true,
            videoUploadPercent: 0,
            videoLoading: false,
            imageLoading: false,
            imageUploadPercent: 0
        }
    },
    created: function() {
        this.pageId = parseInt(this.$route.query.page) || 1;
        this.getData();
        getVideoCategoryLists().then(res => {
            this.videoCateGoryList = res.list;
        })
    },
    methods: {
        getData: function() {
            getVideoLists({
                params: {
                    pageId: this.pageId,
                    pageSize: this.pageSize,
                    videoNameLike: this.videoNameLike,
                    videoCategoryId: this.videoCategoryId
                }
            }).then(res => {
                console.log(res)
                this.videoList = res.list;
                this.count = res.count;
            })
        },
        showDialog: function() {
            this.add = true;
            this.dialogShow = true;
        },
        closeDialog: function() {
            this.dialogShow = false;
            this.add = false;
            this.video = {
                videoId: null,
                videoName: '',
                orderNumber: 1,
                particular: '',
                coverTheAddress: '',
                videoCategoryId: null,
                videoUrl: ''
            }
            this.imageUrl = '';
            this.videoUrl = '';
        },
        save: function() {
            if (this.add) {
                addVideo(this.video).then(res => {
                    this.getData();
                    this.$message({
                        message: '添加成功!',
                        duration: 2000,
                        type: 'success'
                    });
                    this.closeDialog();
                })
            } else {
                updateVideoById(this.video.videoId, this.video).then(res => {
                    this.getData();
                    this.$message({
                        message: '修改成功!',
                        duration: 2000,
                        type: 'success'
                    });
                    this.closeDialog();
                })
            }
        },
        updateDatas: function(index, row) {
            this.video = {
                videoId: row.videoId,
                videoName: row.videoName,
                orderNumber: row.orderNumber,
                particular: row.particular,
                coverTheAddress: row.coverTheAddress,
                videoCategoryId: row.videoCategoryId,
                videoUrl: row.videoUrl
            }
            this.imageUrl = row.coverTheAddress;
            this.videoUrl = row.videoUrl;
            this.add = false;
            this.dialogShow = true;
        },
        deleteDatas: function(index, row) {
            this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                deleteVideoById(row.videoId).then(res => {
                    this.getData();
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        uploadPosterSuccess: function(res) {
            if (res.status) {
                this.$message({
                    message: '上传成功',
                    type: 'success'
                })
                this.imageUrl = res.data.webPath;
                this.video.coverTheAddress = res.data.webPath;
            } else {
                this.$message({
                    message: '上传失败',
                    type: 'error'
                })
            }
            this.imageLoading = false;
        },
        uploadVideoSuccess: function(res) {
            if (res.status) {
                this.$message({
                    message: '上传成功',
                    type: 'success'
                })
                this.videoUrl = res.data.webPath;
                this.video.videoUrl = res.data.webPath;
            } else {
                this.$message({
                    message: '上传失败',
                    type: 'error'
                })
            }
            this.videoLoading = false;
        },
        currentChange: function(val) {
            this.$router.push('?page=' + val);
            this.pageId = val;
            this.getData();
        },
        changeUpload: function(val){
        	if(val){
        		this.video.videoUrl = '';
        		this.videoUrl = '';
        	}
        },
        uploadVideoProgress: function(event, file, fileList){
            this.videoLoading = true;
            this.videoUploadPercent = +file.percentage.toFixed(0);
        },
        uploadImageProgress: function(event, file, fileList){
            this.imageLoading = true;
            this.imageUploadPercent = +file.percentage.toFixed(0);
        }
    }
}
</script>
<style scoped lang="scss">
.title {
    font-size: 12px;
    margin-bottom: 5px;
    h3 {
        display: inline-block;
        padding: 5px 0;
        margin: 0;
        font-weight: normal;
        color: #000;
        border-bottom: 2px solid #000;
    }
}

.poster {
    max-width: 100%;
}

.poster-thumb {
    width: 300px;
}
</style>